#include "Test.h"

sint16 ENC_Get(void)
{
    //CCU6_InitConfig(CCU60, CCU6_Channel0, 200);
    sint16 ECPULSE2 = 0;
    ECPULSE2 = ENC_GetCounter(ENC2_InPut_P02_8);
    return ECPULSE2;
}

void Motor_Speed_Test()
{
    //初始化
    Move_Init();
    GPIO_LED_Init();
    GPIO_KEY_Init();
    Display_Init();
    Display_CLS(U16_WHITE);
    char txt[32];
    uint8 bit_Speed;
    sint16 ECPULSE2;
    sprintf(txt,"Motor_Speed_Test");
    Display_showString(0,0,txt,U16_RED,U16_WHITE,16);

    while(1)
    {
        bit_Speed = 'x';
        if (KEY_Read(KEY0) == 0 || bit_Speed ==  'K') // 按下KEY0键，占空比减小，或者蓝牙控制中的down键
        {
            //LED_Ctrl(LED0,ON);
            //sprintf(txt, "key0 down ");
            if (BLDC_duty > -ATOM_PWM_MAX)
                BLDC_duty -= 50;
            //sprintf(txt, "HALL:%05d  BLDC:%05d  ",Get_Hall_State(),BLDC_duty);
            while(KEY_Read(KEY0) == 0);

        }
        if (KEY_Read(KEY2) == 0 || bit_Speed == 'G') // 按下KEY2键，占空比加大，或者蓝牙控制中的up键
        {
            if (BLDC_duty < ATOM_PWM_MAX) // 满占空比为12500
                BLDC_duty += 50;
            while(KEY_Read(KEY2) == 0);
        }
        if (KEY_Read(KEY1) == 0 || bit_Speed == 'I') // 按下KEY1键，占空比中值
        {
            BLDC_duty = 500;
        }
        LQ_BLDCCtrl();
        ECPULSE2 = ENC_Get();
        sprintf(txt, "Enc2: %05d;", ECPULSE2);
        Display_showString(0, 4, txt, U16_WHITE, U16_BLACK, 16); // 字符串显示
        sprintf(txt, "HALL:%05d  BLDC:%05d  ",Get_Hall_State(),BLDC_duty);
        Display_showString(0, 3, txt, U16_RED, U16_BLUE, 16); // 将txt中 内容显示出来
    }
}
